module nade.hud;

import std.stdio;

import nade.vector;
import nade.region;
import nade.clipper;


class HUD {
	void render()
	{
		foreach(frame; _frames)
		{
			scope clipper = new Clipper(frame.region.min.as!(Vector2), frame.region.max.as!(Vector2), true);
			frame.frame.render(frame.region);
		}
	}

	Region2 show(IFrame frame, const Region2 area)
	{
		Region2 marea = area.dup;
		const min = frame.sizeMin;
		const max = frame.sizeMax;

		if(marea.width > max.x) marea.width = max.x;
		if(marea.height > max.y) marea.height = max.y;
		if(marea.width < min.x) marea.width = min.x;
		if(marea.height < min.y) marea.height = min.y;

		_frames ~= FramedRegion(frame, marea);

		return marea;
	}
	Region2 show(IFrame frame, const Vector2i position)
	{
		auto marea = Region2(position, position + frame.sizeMin);

		_frames ~= FramedRegion(frame, marea);

		return marea;
	}
	void resize(IFrame frame, Vector2i size)
	{
		foreach(ref child; _frames)
			if(child.frame is frame)
				child.region.dimensions = size;
	}

	void hide()
	{
		_frames.length = 0;
	}
	void hide(IFrame frame)
	{
		for(int i = 0;  i < _frames.length;  ++i)
		{
			if(_frames[i].frame is frame)
			{
				_frames = _frames[0 .. i] ~ _frames[(i + 1) .. $];
				return;
			}
		}
	}

	bool click(Vector2 position)
	{
		foreach(frame; _frames)
		{
			if(frame.region.contains(position)  &&  frame.frame.click(position - frame.region.min))
				return true;
		}

		return false;
	}

	void reposition(Vector2i offset)
	{
		foreach(ref frame; _frames)
		{
			frame.region.min = frame.region.min + offset;
			frame.region.max = frame.region.max + offset;
		}
	}

	// Region2 childArea()
	// {
		// auto result = Region2(int.max, int.max, -int.max, -int.max);
		// foreach(ref frame; _frames)
		// {
		// }
	// }

	private {
		FramedRegion[]		_frames;

		struct FramedRegion {
			IFrame		frame;
			Region2		region;
		}
	}
}


interface IFrame {
	void render(const Region2 area);
	bool click(Vector2 position);

	Vector2i sizeMin();
	Vector2i sizeMax();
	Region2 scrollArea(const Region2 area);
}
